﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class RoleRepository : IRoleRepository
    {
        private readonly AgricultureDbContext _dbContext;
        public RoleRepository(AgricultureDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<Role>> GetRolesAsync()
        {
            var list = _dbContext.Roles.Where(p => p.Remark == "未删除");
            return await list.ToListAsync();
        }
        /// <summary>
        ///  添加角色
        /// </summary>
        /// <param name="role"> 角色实体 </param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddRole(Role role)
        {
            await _dbContext.Roles.AddAsync(role);
            return await _dbContext.SaveChangesAsync();
        }
        /// <summary>
        ///  删除角色
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        public async Task<int> DeleteRole(int id)
        {
            var role = await _dbContext.FindAsync<Role>(id);
            role.Remark = "已删除";
            _dbContext.Roles.Update(role!);
            return await _dbContext.SaveChangesAsync();
        }
        /// <summary>
        /// 获取角色选择列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<Role>> GetRoleSelect()
        {
            var role = await _dbContext.Roles.ToListAsync();
            return role;
        }
    }
}
